﻿using SSC.Models;
using System.Windows;
using System.Xml.Linq;

namespace SSC.Views
{
    /// <summary>
    /// wdAddMaintenance.xaml 的交互逻辑
    /// </summary>
    public partial class wdAddMaintenance : Window
    {
        public MaintenanceItemModel Result { get; private set; }

        public wdAddMaintenance()
        {
            InitializeComponent();
        }
        public wdAddMaintenance(MaintenanceItemModel itemToEdit)
        {
            InitializeComponent();
            // 将 itemToEdit 的属性填充到各输入框中，例如：
            txtItemName.Text = itemToEdit.ItemName;
            txtCycleDays.Text = itemToEdit.CycleDays.ToString();
            txtDescription.Text = itemToEdit.Description;

            // 你可以将 itemToEdit 复制到 Result 或创建新副本
            Result = itemToEdit;
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtItemName.Text) ||
                !int.TryParse(txtCycleDays.Text, out int days))
            {
                MessageBox.Show("请填写名称和有效的周期天数", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            Result = new MaintenanceItemModel
            {
                Id = Result?.Id ?? 0,
                ItemName = txtItemName.Text.Trim(),
                CycleDays = days,
                Description = txtDescription.Text.Trim(),
                LastMaintainAt = DateTime.Now
            };

            DialogResult = true;
            Close();
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
}
